
@extends('common.header')

<!-- 添加meta csrf_token -->
<meta name="csrf-token" content="{{ csrf_token() }}">  
<link rel="stylesheet" type="text/css" href="{{ asset('/css/auth.css') }}"/>
<script src=" {{ asset('/js/jquery-1.8.3.min.js') }} "></script>
<script type="text/javascript">
	
	<!-- 2.设置全局ajax选项 -->
	$.ajaxSetup({
		headers: {
			'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
		}
	});
	// 获取 token
	var _token = $('meta[name="csrf-token"]').attr('content');
	
	
		
</script>
<body>

<br/>
<br/>
<br/>
<br/>
<br/>


<div class="col-xs-12 col-sm-6 col-md-1">
</div>	
<!-- 页面标记  -->

<!-- 点击添加的弹出框 开始-->
<div style="width:1000px;position:fixed;top:52px;left:50%;margin-left:-500px;z-index:1000;height:40px;background:#c9302c;display:none;color:#fff;line-height:40px;font-size:18px;text-align:center" class="opa"></div>


<!-- 点击添加的弹出框 结束-->
<div class="wrap">
	<div style="overflow:hidden">
	 @forelse( $controllers as $key => $controller )
			<div class="group1">
				<div class="box">
					<h2>{{ $controller->desc }}</h2>
					
				</div>
				<ul id="list">
					@forelse( $functions[$key] as $k => $func )
						<li>
							<label>
								<input type="checkbox" value="{{ $func['id'] }}" @if( in_array($func['id'],$auth) ) checked @else @endif  />@if( in_array($func['id'],$auth) ) <span style="color:red;">{{ $func['name'] }}</span> @else {{ $func['name'] }} @endif
							</label>
						</li>
					@empty
					@endforelse
				</ul>
			</div>
			@empty
			@endforelse
	</div>
       
	<div class="opera">
		<label><input type="checkbox" value="all" id="all">全选</label>
		<label><input type="checkbox" value="reverse" class="btn" id="reverse" style="margin-top:-4px">反选</label>
	</div>
	<br/><br/><br/>
	<div style="overflow:hidden;width:450px;margin:0 auto;">
   
		<button  style="width:200px;float:left;" class="btn btn-danger">保存</button>

		<div style="margin-left:50px;float:left;margin-bottom:200px;">
			<a href ="javascript:history.back();" style="width:200px;" class="btn btn-success">
			返回
			</a>
		</div>
		
	</div>
		
		
</div>

<script type="text/javascript">
	
	// 
	function allchk(){
		var chknum = $(".group1 :checkbox").size();//选项总个数
		var chk = 0;
		$(".group1 :checkbox").each(function () {
			if($(this).prop("checked")==true){
				chk++;
			}
		});
		if(chknum==chk)
		{
			//全选
			$("#all").prop("checked",true);
		}else{
			//不全选
			$("#all").prop("checked",false);
		}
	}
	
	// 全选
	$("#all").click(function(){
		if(this.checked){
			$(".group1 :checkbox").prop("checked", true);
		}else{
			$(".group1 :checkbox").prop("checked", false);
		}
	});
	

	//反选
	$("#reverse").click(function () {
		$(".group1 :checkbox").each(function () {
			$(this).prop("checked", !$(this).prop("checked"));
		});
		allchk();
	});

	//设置全选复选框
	$(".group1 :checkbox").click(function(){
		allchk();
	});

         
	//点击保存
	$(".btn-danger").click(function(){
		var all	 =	[];
		var key	 =	0;
		$("input[type=checkbox]:checked").each(function(){
			// 遍历 获取每个选中框的值 评论ID
			var id = $(this).val();
			if( id!= 'all' && id !='reverse' )
			{
				++key;
				all[key] = id ;
			}
		});
		if( key==0 ){
			alert('没有选中任何权限');
			location.reload();
			return false;
		}
		
		
		$.ajax({
			type:"post",
			url:"{{ url('/addauth') }}",
			data:{all:all,admin_id:{{ $admin_id }},_token:_token},
			async:true,    					
			dataType:"json",
			success:function(back){
				if( back['show_info']=='权限更改成功' )
				{
					$(".btn-danger").attr('disabled',true);
				}
				$(".opa").html(back['show_info']);
				$(".opa").slideDown(1000);
				$(".opa").delay(1500).slideUp(1000);
				
			}
		});
		
		
		
	})


</script>

</body>
@extends('common.footer')